package com.flyduck.mall.pms.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.mall.common.core.utils.BeanUtils;
import com.flyduck.mall.pms.admin.vo.*;
import com.flyduck.mall.pms.domain.PmsAd;
import com.flyduck.mall.pms.mapper.PmsAdMapper;
import com.flyduck.ss.common.core.utils.StringUtils;
import com.flyduck.mall.common.mybatis.utils.PageUtils;
import com.flyduck.mall.pms.admin.service.AdminAdService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @program: vueshop
 * @description:
 * @author: flyduck
 * @create: 2024-06-03 21:44
 **/
@Service
public class AdminAdServiceImpl implements AdminAdService {

    @Resource
    private PmsAdMapper pmsAdMapper;

    @Override
    public Page<AdminAdRespVO> getAdPage(Page page, AdminAdQueryReqVO adminAdQueryReqVO) {
        Page<PmsAd> appAdPage = pmsAdMapper.selectPage(page,
                new LambdaQueryWrapper<PmsAd>()
                        .like(StringUtils.isNotBlank(adminAdQueryReqVO.getName()), PmsAd::getName, adminAdQueryReqVO.getName())
                        .orderByDesc(PmsAd::getCreateTime)
        );

        return PageUtils.convert(appAdPage, AdminAdRespVO.class);
    }

    @Override
    public AdminAdDetailsRespVO getAdDetailsById(Long id) {
        PmsAd pmsAd = pmsAdMapper.selectById(id);
        return BeanUtils.toBean(pmsAd, AdminAdDetailsRespVO.class);
    }

    @Override
    public void saveAd(AdminAdSaveReqVO adminAdSaveReqVO) {
        PmsAd pmsAd = BeanUtils.toBean(adminAdSaveReqVO, PmsAd.class);
        pmsAd.setCreateTime(new Date());
        pmsAdMapper.insert(pmsAd);
    }

    @Override
    public void updateAd(AdminAdUpdateReqVO adminAdUpdateReqVO) {
        PmsAd pmsAd = BeanUtils.toBean(adminAdUpdateReqVO, PmsAd.class);
        pmsAd.setUpdateTime(new Date());
        pmsAdMapper.updateById(pmsAd);
    }

    @Override
    public void batchDeleteByIds(List<Long> ids) {
        pmsAdMapper.deleteBatchIds(ids);
    }
}
